package com.label.Web;

import com.alibaba.fastjson.JSONObject;
import com.label.Config.MyBatis.IdGenerator;
import com.label.Dao.MessageInfoMapper;
import com.label.Dao.MessageMapper;
import com.label.Model.Common.ResponseBean;
import com.label.Model.Query.*;
import com.label.Service.TaskService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/task")
public class TaskController {

    private final static Logger logger = LoggerFactory.getLogger(TaskController.class);

    @Resource
    private TaskService taskService;

    @Value("${file.path}")
    public String path;

    @Resource
    private MessageMapper messageMapper;

    @Resource
    private MessageInfoMapper messageInfoMapper;

    @RequestMapping("/getUserTree")
    public ResponseBean getUserTree(){
        try{
            return new ResponseBean(HttpStatus.OK.value(),"success",taskService.getStuUsers());
        } catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return new ResponseBean(500,"失败",null);
        }
    }

    @RequestMapping("/addTask")
    public ResponseBean addTask(@RequestBody TaskQuery taskQuery){
        try{
            return new ResponseBean(HttpStatus.OK.value(),"success",taskService.addTask(taskQuery));
        } catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return new ResponseBean(500,"失败",null);
        }
    }

    @RequestMapping("/AddTask")
    public void AddTask(@RequestBody TaskQuery taskQuery){

        List<Map<String,Object>> userGroups=(List<Map<String,Object>>)JSONObject.parse(taskQuery.getReceiverList().get("groupList").toString());
        System.out.println(userGroups);
    }

    @RequestMapping("/getAllTeacherTask")
    @RequiresAuthentication
    public ResponseBean getAllTeacherTask(){
        try{
            return new ResponseBean(HttpStatus.OK.value(),"success",taskService.getAllTeacherTask());
        } catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return new ResponseBean(500,"失败",null);
        }
    }

    @RequestMapping("/getAllStuTask")
    @RequiresAuthentication
    public ResponseBean getAllStuTask(){
        try{
            return new ResponseBean(HttpStatus.OK.value(),"success",taskService.getAllStuTask());
        } catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return new ResponseBean(500,"失败",null);
        }
    }

    //    修改学生通知的状态为"已确认"
    @RequestMapping("/updateState")
    @RequiresAuthentication
    public ResponseBean updateState(@RequestBody TaskDetailQuery taskDetailQuery){
        try{
            return new ResponseBean(HttpStatus.OK.value(),"success",taskService.updateState(taskDetailQuery));
        } catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return new ResponseBean(500,"失败",null);
        }
    }

    //    增加提问，并修改通知状态
    @RequestMapping("/addQuestion")
    @RequiresAuthentication
    public ResponseBean addQuestion(@RequestBody TaskQuestionQuery taskQuestionQuery){
        try{
            return new ResponseBean(HttpStatus.OK.value(),"success",taskService.addQuestion(taskQuestionQuery));
        } catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return new ResponseBean(500,"失败",null);
        }
    }

    //    回复问题并且通知提问学生
    @RequestMapping("/answerQuestion")
    @RequiresAuthentication
    public ResponseBean answerQuestion(@RequestBody TaskQuestionQuery taskQuestionQuery){
        try{
            return new ResponseBean(HttpStatus.OK.value(),"success",taskService.answerQuestion(taskQuestionQuery));
        } catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return new ResponseBean(500,"失败",null);
        }
    }

    //    删除任务
    @RequestMapping("/delTask")
    @RequiresAuthentication
    public ResponseBean delTask(@RequestBody TaskQuery taskQuery){
        try{
            if(taskService.delTask(taskQuery))
                return new ResponseBean(HttpStatus.OK.value(),"success",true);
            return new ResponseBean(500,"失败",null);
        } catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return new ResponseBean(500,"失败",null);
        }
    }

    //    删除问题
    @RequestMapping("/delQuestion")
    @RequiresAuthentication
    public ResponseBean delQuestion(@RequestBody TaskQuestionQuery taskQuestionQuery){
        try{
            if(taskService.delQuestion(taskQuestionQuery))
                return new ResponseBean(HttpStatus.OK.value(),"success",true);
            return new ResponseBean(500,"失败",null);
        } catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return new ResponseBean(500,"失败",null);
        }
    }

    //    删除问题
    @RequestMapping("/sendTeacher")
    @RequiresAuthentication
    public ResponseBean sendTeacher(@RequestBody TaskQuery taskQuery){
        try{
            if(taskService.sendTeacher(taskQuery))
                return new ResponseBean(HttpStatus.OK.value(),"success",true);
            return new ResponseBean(500,"失败",null);
        } catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return new ResponseBean(500,"失败",null);
        }
    }

    @RequestMapping("/getAllClassAndTemplate")
    @RequiresAuthentication
    public ResponseBean getAllClassAndTemplate(){
        try{
            return new ResponseBean(HttpStatus.OK.value(),"success",taskService.getAllClassAndTemplate());
        } catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return new ResponseBean(500,"失败",null);
        }
    }

    @RequestMapping("/addTemplate")
    @RequiresAuthentication
    public ResponseBean addTemplate(@RequestBody TemplateQuery templateQuery){
        try{
            if(taskService.addTemplate(templateQuery))
                return new ResponseBean(HttpStatus.OK.value(),"success",true);
            return new ResponseBean(500,"失败",null);
        } catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return new ResponseBean(500,"失败",null);
        }
    }

    @RequestMapping("/updateTemplate")
    @RequiresAuthentication
    public ResponseBean updateTemplate(@RequestBody TemplateQuery templateQuery){
        try{
            if(taskService.updateTemplate(templateQuery))
                return new ResponseBean(HttpStatus.OK.value(),"success",true);
            return new ResponseBean(500,"失败",null);
        } catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return new ResponseBean(500,"失败",null);
        }
    }

    @RequestMapping("/getOneTemplate")
    @RequiresAuthentication
    public ResponseBean getOneTemplate(@RequestBody TemplateQuery templateQuery){
        try{
            return new ResponseBean(HttpStatus.OK.value(),"success",taskService.getOneTemplate(templateQuery));
        } catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return new ResponseBean(500,"失败",null);
        }
    }

    @RequestMapping("/uploadFile")
    @RequiresAuthentication
    public ResponseBean uploadFile(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return new ResponseBean(500,"上传失败，请选择文件",null);
        }

        File dest = new File(path + IdGenerator.uuid()+file.getOriginalFilename());
        try {
            file.transferTo(dest);
            String picPath = "http://10.1.40.84:7878/file/"+ URLEncoder.encode(dest.getName(), "UTF-8");
            return new ResponseBean(200,"上传成功",picPath);
        } catch (IOException e) {
            return new ResponseBean(400,"上传失败",null);
        }
    }
}
